package org.jsets.fastboot.oss.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Objects;
import org.jsets.fastboot.model.BaseResp;
import org.jsets.fastboot.model.DataResp;
import org.jsets.fastboot.model.ListResp; 
import org.jsets.fastboot.oss.model.dto.StorageQuery;
import org.jsets.fastboot.oss.model.entity.Storage;
import org.jsets.fastboot.oss.service.IBucketService;
import org.jsets.fastboot.oss.service.IStorageConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@RestController
@RequestMapping("/oss/storages")
@Api(tags = "存储引擎")
public class StorageController {
	
	@Autowired
	private IStorageConfigService service;
	@Autowired
	private IBucketService bucketService;
	
	@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("增加存储引擎")
	public BaseResp insert(@RequestBody @Validated Storage entity) {
		Storage present = this.service.getByName(entity.getName());
		if (Objects.nonNull(present)) {
			throw new IllegalArgumentException("存在名称为["+entity.getName()+"]的存储引擎，请更换");
		}
		this.service.insertStorage(entity);
		return BaseResp.ok();
	}
	
	@PutMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("修改存储引擎")
	public BaseResp update(@RequestBody @Validated Storage entity) {
		Storage present = this.service.getByName(entity.getName());
		if (Objects.nonNull(present) && !present.getId().equals(entity.getId())) {
			throw new IllegalArgumentException("存在名称为[" + entity.getName() + "]的存储引擎，请更换");
		}

		this.service.updateStorage(entity);
		return BaseResp.ok();
	}
	
	@DeleteMapping(value="/{id}" ,produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("删除存储引擎")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="主键", required=true, dataType = "Long", dataTypeClass = Long.class, paramType="path")
	})
	public BaseResp delete(@PathVariable("id") String id) {
		Objects.requireNonNull(id, "主键[id]不能为空");
		if (this.bucketService.selectCountByStorage(id) > 0) {
			throw new IllegalArgumentException("有'桶'正在使用该引擎，请先删除关联的桶");
		}
		this.service.deleteStorage(id);
		return BaseResp.ok();
	}
	
	@GetMapping(value="/{id}" ,produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("获取存储引擎")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="主键", required=true, dataType = "String", dataTypeClass = String.class, paramType="path")
	})
	public DataResp<Storage> get(@PathVariable("id") String id) {
		Storage entity = this.service.getById(id);
		DataResp<Storage> resp = DataResp.ok();
		resp.setData(entity);
		return resp;
	}

	@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("查询存储引擎")
	@ApiImplicitParams({
		 @ApiImplicitParam(name="page_no", value="当前页码", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="page_size", value="每页条数", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="name", value="名称", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="type", value="类型", required=false, dataType = "Long", dataTypeClass = Long.class, paramType="query")
	})
	public ListResp<Storage> list(
			@RequestParam(name = "page_no", required = false) Integer pageNo,
			@RequestParam(name = "page_size", required = false) Integer pageSize,
			@RequestParam(name = "name", required = false) String name,
			@RequestParam(name = "type", required = false) Integer type) {

		StorageQuery criteria = new StorageQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setName(name)
				.setType(type);
		
		ListResp<Storage> respond = ListResp.ok();
		if(criteria.isPageNecessary()) {
			Page<Storage> result = this.service.selectPage(criteria);
			respond.setData(result.getRecords());
			respond.setTotalCount(result.getTotal());
		}else {
			respond.setData(this.service.selectList(criteria));
		}
		return respond;
	}

}
